Radar Host Communication library  2020.08.13
Protocol.c
Go to the documentation of this file.
1 
14 /* ===========================================================================
15 ** Copyright (C) 2016-2020 Infineon Technologies AG
16 ** All rights reserved.
17 ** ===========================================================================
18 **
19 ** ===========================================================================
20 ** This document contains proprietary information of Infineon Technologies AG.
21 ** Passing on and copying of this document, and communication of its contents
22 ** is not permitted without Infineon's prior written authorisation.
23 ** ===========================================================================
24 */
25 
26 /*
27 ==============================================================================
28 1. INCLUDE FILES
29 ==============================================================================
30 */
31 #define _CRT_SECURE_NO_WARNINGS
32 #include "Protocol.h"
33 #define __PROTOCOL_INCLUDE_ENDPOINT_ONLY_API__
34 #include "Protocol_internal.h"
35 #undef __PROTOCOL_INCLUDE_ENDPOINT_ONLY_API__
36 #include "COMPort.h"
37 
38 #include <stddef.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 
43 #undef _CRT_SECURE_NO_WARNINGS
44 /*
45 ==============================================================================
46  2. LOCAL DEFINITIONS
47 ==============================================================================
48 */
49 
61 #define CNST_STARTBYTE_DATA 0x5A
63 #define CNST_STARTBYTE_STATUS 0x5B
65 #define CNST_END_OF_PAYLOAD 0xE0DB
70 #define CNST_MSG_QUERY_ENDPOINT_INFO 0x00
74 #define CNST_MSG_ENDPOINT_INFO 0x00
78 #define CNST_MSG_QUERY_FW_INFO 0x01
82 #define CNST_MSG_FW_INFO 0x01
86 #define CNST_MSG_FIRMWARE_RESET 0x02
90 #define CNST_PROTOCOL_RECEIVED_PAYLOAD_MSG 0x01000000
91 
96 /*
97 ==============================================================================
98  3. LOCAL TYPES
99 ==============================================================================
100 */
101 
104 typedef struct
105 {
106  uint32_t type;
107  uint16_t version;
109  const Endpoint_Definition_t* endpoint_definiton;
114 } Endpoint_t;
115 
117 
119 
133 typedef struct
134 {
135  void* port_handle;
136  uint8_t num_endpoints;
138  Endpoint_t* endpoints;
143  size_t(*get_data)(void* port_handle, void* data, size_t num_requested_bytes);
144 
146  size_t (*send_data)(void* port_handle, const void* data, size_t num_bytes);
147 
149  void (*close)(void* port_handle);
150 } Instance_t;
151 
153 
155 
163 typedef struct
164 {
165  uint8_t endpoint;
167  uint8_t* payload;
169  uint16_t payload_size;
170 } Message_Info_t;
171 
172 typedef struct
173 {
174  uint8_t is_udp;
175  uint8_t is_tcp;
176  int port;
177  char ip4addr[16];
178 
179 } Port_Info_t;
181 
182 /*
183 ==============================================================================
184  4. DATA
185 ==============================================================================
186 */
187 
213 static Instance_t* handles = NULL;
215 static size_t num_allocated_handles = 0;
217 static size_t num_open_handles = 0;
222 /* Include list of known endpoints (see Protocol_KnownEndpoints.h for details)
223  */
224 #define __PROTOCOL_INCLUDES_ENDPOINT_LIST__
225 #include "Protocol_KnownEndpoints.h"
226 #undef __PROTOCOL_INCLUDES_ENDPOINT_LIST__
227 
235 #define NUM_KNOWN_ENDPOINTS \
236  (sizeof(known_endpoints) / sizeof(const Endpoint_Definition_t*))
237 
238 /*
239 ==============================================================================
240  5. LOCAL FUNCTION PROTOTYPES
241 ==============================================================================
242 */
243 
259 static void recover_from_receive_error(const Instance_t* protocol);
260 
280 static void send_message(const Instance_t* protocol, uint8_t endpoint,
281  const uint8_t* payload, uint16_t payload_size);
282 
312 static int32_t get_message(const Instance_t* protocol,
313  Message_Info_t* message_info);
314 
315 /*
316 ==============================================================================
317  6. LOCAL FUNCTIONS
318 ==============================================================================
319 */
320 
322 
323 
325 static void recover_from_receive_error(const Instance_t* protocol)
326 {
327  /* read until buffer is empty */
328  uint8_t dummy_data[1024];
329  size_t received_bytes = sizeof(dummy_data);
330 
331  while (received_bytes == sizeof(dummy_data))
332  {
333  received_bytes = protocol->get_data(protocol->port_handle,
334  dummy_data, sizeof(dummy_data));
335  }
336 
337  /* now we have run out of data, protocol should be in sync again */
338 }
339 
340 static void send_message(const Instance_t* protocol, uint8_t endpoint,
341  const uint8_t* payload, uint16_t payload_size)
342 {
343  /* setup message header and tail */
344  uint8_t message_header[4];
345  uint8_t message_tail[2];
346 
347  message_header[0] = CNST_STARTBYTE_DATA;
348  message_header[1] = endpoint;
349  message_header[2] = payload_size & 0xFF;
350  message_header[3] = payload_size >> 8;
351 
352  message_tail[0] = CNST_END_OF_PAYLOAD & 0xFF;
353  message_tail[1] = CNST_END_OF_PAYLOAD >> 8;
354 
355  /* allocate memory */
356  size_t message_size = (size_t)4 + (size_t)payload_size + (size_t)2;
357  uint8_t* message = malloc(message_size *sizeof(uint8_t));
358  if (message == NULL)
359  return;
360 
361  /* copy header, payload, and tail into one buffer */
362  memcpy(message, message_header, sizeof(message_header));
363  memcpy(message+sizeof(message_header), payload, payload_size);
364  memcpy(message + sizeof(message_header) + payload_size, message_tail, sizeof(message_tail));
365 
366  /* send message */
367  protocol->send_data(protocol->port_handle, message, message_size);
368 
369  free(message);
370 }
371 
372 static int32_t get_message(const Instance_t* protocol,
373  Message_Info_t* message_info)
374 {
375  uint8_t message_header[4];
376  size_t num_received_bytes;
377 
378  // message_info should have valid values even if we return from this
379  // function with an error code.
380  message_info->endpoint = 0;
381  message_info->payload = NULL;
382  message_info->payload_size = 0;
383 
384  /* read message header */
385  /* ------------------- */
386  num_received_bytes = protocol->get_data(protocol->port_handle,
387  message_header,
388  sizeof(message_header));
389 
390  /*
391  * If number of received bytes is smaller than expected, one reason could
392  * be that the message came in shortly before the timeout. If this has
393  * happened, the rest of the message bytes must be available now. Try
394  * another read.
395  */
396  if ((num_received_bytes < sizeof(message_header)))
397  {
398  num_received_bytes +=
399  protocol->get_data(protocol->port_handle,
400  message_header + num_received_bytes,
401  sizeof(message_header) - num_received_bytes);
402  }
403 
404  /* if no data at all has been received, everything is all right, but there
405  * is no message
406  */
407  if (num_received_bytes == 0)
408  {
410  }
411 
412  /* if not enough bytes for a full message header have been received,
413  * something went wrong
414  */
415  else if (num_received_bytes < sizeof(message_header))
416  {
417  recover_from_receive_error(protocol);
419  }
420 
421  /* read rest of message */
422  /* -------------------- */
423  if (message_header[0] == CNST_STARTBYTE_DATA)
424  {
425  uint8_t* payload;
426  uint16_t payload_size;
427  uint8_t message_tail[2];
428 
429  /* allocate memory and receive payload */
430  payload_size = (uint16_t)message_header[2] |
431  ((uint16_t)message_header[3]) << 8;
432  payload = (uint8_t*)malloc(payload_size);
433 
434  num_received_bytes = protocol->get_data(protocol->port_handle,
435  payload, payload_size);
436 
437  /* check if payload has been received completely */
438  if (num_received_bytes < payload_size)
439  {
440  /* free payload memory, it's incomplete */
441  free(payload);
442 
443  recover_from_receive_error(protocol);
444 
446  }
447 
448  /* check message tail */
449  num_received_bytes = protocol->get_data(protocol->port_handle,
450  &message_tail,
451  sizeof(message_tail));
452 
453  if ((num_received_bytes != sizeof(message_tail)) ||
454  (message_tail[0] != (CNST_END_OF_PAYLOAD & 0xFF)) ||
455  (message_tail[1] != CNST_END_OF_PAYLOAD >> 8))
456  {
457  /* free payload memory, it's incomplete */
458  free(payload);
459 
460  recover_from_receive_error(protocol);
461 
463  }
464 
465  message_info->endpoint = message_header[1];
466  message_info->payload = payload;
467  message_info->payload_size = payload_size;
468 
470  }
471  else if (message_header[0] == CNST_STARTBYTE_STATUS)
472  {
473  uint8_t endpoint = message_header[1];
474  uint16_t status_code = (uint16_t)message_header[2] |
475  ((uint16_t)message_header[3]) << 8;
476 
477  return ((int)endpoint << 16) | status_code;
478  }
479  else
480  {
481  recover_from_receive_error(protocol);
483  }
484 }
485 
496 static char* strclone(const char* s)
497 {
498  size_t size = strlen(s) + 1;
499  char* copy = calloc(size, sizeof(char));
500  if (copy)
501  memcpy(copy, s, size);
502  return copy;
503 }
504 
505 Port_Info_t parse_port_name(const char* port_name)
506 {
507  Port_Info_t port_info = { 0 };
508 
509  if (strlen(port_name) <= 6)
510  return port_info;
511 
512  if (strncmp(port_name, "udp://", 6) == 0)
513  {
514 
515  port_info.is_udp = 1;
516  }
517  else if (strncmp(port_name, "tcp://", 6) == 0)
518  {
519  port_info.is_tcp = 1;
520  }
521  else
522  {
523  return port_info;
524  }
525 
526  char* p = NULL;
527  char* server_port = strclone(port_name + 6);
528 
529  if (server_port)
530  {
531  p = strchr(server_port, ':');
532  if (p)
533  {
534  *p = '\0';
535  if (strlen(server_port) <= sizeof(port_info.ip4addr))
536  {
537  strcpy(port_info.ip4addr, server_port);
538  port_info.port = atoi(p + 1);
539  }
540  }
541 
542  }
543 
544  free(server_port);
545  return port_info;
546 }
547 
548 /*
549 ==============================================================================
550  7. EXPORTED FUNCTIONS
551 ==============================================================================
552 */
553 
554 int32_t protocol_connect(const char* port_name)
555 {
556  Instance_t protocol_instance = { 0 };
557  Message_Info_t message_info;
558  int32_t receive_status;
559  int32_t new_handle;
560  {
561  /* open the COM port */
562  /* ----------------- */
563  com_t* com_port_handle = com_open(port_name);
564  if(!com_port_handle)
566 
567  protocol_instance.port_handle = com_port_handle;
568  protocol_instance.get_data = (size_t(*)(void*, void*, size_t))com_get_data;
569  protocol_instance.send_data = (size_t (*)(void*, const void*, size_t))com_send_data;
570  protocol_instance.close = (void (*)(void*))com_close;
571  }
572 
573  /* get Endpoint information from device */
574  /* ------------------------------------ */
575  protocol_instance.num_endpoints = 0;
576  protocol_instance.endpoints = NULL;
577 
578  /* send a message with command code to query endpoint info to endpoint 0
579  */
580  {
581  uint8_t uQueryMessage[1] = { CNST_MSG_QUERY_ENDPOINT_INFO };
582  send_message(&protocol_instance, 0, uQueryMessage, sizeof(uQueryMessage));
583  }
584 
585  /* read and parse reply message from connected device */
586  /* -------------------------------------------------- */
587  message_info.payload = NULL;
588  receive_status = get_message(&protocol_instance, &message_info);
589 
590  if ((receive_status != CNST_PROTOCOL_RECEIVED_PAYLOAD_MSG) ||
591  (message_info.endpoint != 0) ||
592  (message_info.payload_size < 2) ||
593  (message_info.payload[0] != CNST_MSG_ENDPOINT_INFO))
594  {
595  /* This is not the expected payload, clean up and quit. */
596  free(message_info.payload);
597  protocol_instance.close(protocol_instance.port_handle);
599  }
600 
601  /* read number of endpoints and check message size */
602  protocol_instance.num_endpoints = message_info.payload[1];
603 
604  if ((message_info.payload_size !=
605  6 * protocol_instance.num_endpoints + 2) ||
606  (protocol_instance.num_endpoints == 0))
607  {
608  free(message_info.payload);
609  protocol_instance.close(protocol_instance.port_handle);
611  }
612 
613  /* allocate array to hold endpoint information */
614  protocol_instance.endpoints =
615  (Endpoint_t*)malloc(protocol_instance.num_endpoints *
616  sizeof(Endpoint_t));
617 
618  /* iterate over all endpoint info records in the message */
619  for (uint8_t i = 0; i < protocol_instance.num_endpoints; ++i)
620  {
621  uint32_t j;
622  Endpoint_t* endpoint = &protocol_instance.endpoints[i];
623 
624  /* read endpoint type and version from payload */
625  endpoint->type = protocol_read_payload_uint32(message_info.payload,
626  2 + i * 6);
627  endpoint->version = protocol_read_payload_uint16(message_info.payload,
628  6 + i * 6);
629 
630  /* find endpoint implementation matching type and version */
631  endpoint->endpoint_definiton = NULL;
632 
633  for (j = 0; j < NUM_KNOWN_ENDPOINTS; ++j)
634  {
635  if ((known_endpoints[j]->type == endpoint->type) &&
636  (known_endpoints[j]->min_version <= endpoint->version) &&
637  (known_endpoints[j]->max_version >= endpoint->version))
638  {
639  endpoint->endpoint_definiton = known_endpoints[j];
640  break;
641  }
642  }
643  }
644 
645  /* free payload memory */
646  free(message_info.payload);
647  message_info.payload = NULL;
648 
649  /* consume the expected status message */
650  receive_status = get_message(&protocol_instance, &message_info);
651 
652  if (receive_status != ((/*endpoint*/0 << 16) | /*status code*/0x0000))
653  {
654  /*
655  * If the next message was not a status message with Code 0, something
656  * went wrong, so remove all the endpoints, close interface and
657  * return.
658  */
659  if (receive_status == CNST_PROTOCOL_RECEIVED_PAYLOAD_MSG)
660  {
661  free(message_info.payload);
662  }
663 
664  free(protocol_instance.endpoints);
665  protocol_instance.close(protocol_instance.port_handle);
667  }
668 
669  /* add the handle to the array of open COM ports */
670  /* --------------------------------------------- */
671 
672  /* increase capacity of handle array if needed */
673  if (num_open_handles == num_allocated_handles)
674  {
675  Instance_t* pNewHandles = (Instance_t*)malloc((num_open_handles + 1) *
676  sizeof(Instance_t));
677 
678  if (num_allocated_handles > 0)
679  {
680  memcpy(pNewHandles, handles,
681  sizeof(Instance_t) * num_allocated_handles);
682  }
683 
684  pNewHandles[num_allocated_handles++].num_endpoints = 0;
685 
686  free(handles);
687  handles = pNewHandles;
688  }
689 
690  /* add new handle to table */
691  new_handle = 0;
692 
693  while (handles[new_handle].num_endpoints != 0)
694  {
695  ++new_handle;
696  }
697 
698  handles[new_handle] = protocol_instance;
699  ++num_open_handles;
700 
701  return new_handle;
702 }
703 
704 void protocol_disconnect(int32_t protocol_handle)
705 {
706  /* check if handle is valid */
707  if ((protocol_handle >= 0) &&
708  (protocol_handle < (int32_t)num_allocated_handles) &&
709  (handles[protocol_handle].num_endpoints != 0))
710  {
711  const Instance_t* instance = &handles[protocol_handle];
712  /* close COM port */
713  instance->close(instance->port_handle);
714  handles[protocol_handle].port_handle = NULL;
715 
716  /* free memory of endpoint table */
717  free (handles[protocol_handle].endpoints);
718  handles[protocol_handle].endpoints = NULL;
719 
720  /* remove handle from the table */
721  handles[protocol_handle].num_endpoints = 0;
722  --num_open_handles;
724  /* if all handles have been closed, free handle table to prevent
725  * memory leaks
726  */
727  if (num_open_handles == 0)
728  {
729  num_allocated_handles = 0;
730  free(handles);
731  handles = NULL;
732  }
733  }
734 }
735 
736 int32_t protocol_get_num_endpoints(int32_t protocol_handle)
737 {
738  /* check if handle is valid */
739  if ((protocol_handle < 0) ||
740  (protocol_handle >= (int32_t)num_allocated_handles) ||
741  (handles[protocol_handle].num_endpoints == 0))
742  {
744  }
745 
746  return handles[protocol_handle].num_endpoints;
747 }
748 
749 int32_t protocol_get_firmware_information(int32_t protocol_handle,
750  Firmware_Information_t* information)
751 {
752  Instance_t* protocol;
753  Message_Info_t message_info;
754  int32_t receive_status;
756  /* check if handle is valid */
757  if ((protocol_handle < 0) ||
758  (protocol_handle >= (int32_t)num_allocated_handles) ||
759  (handles[protocol_handle].num_endpoints == 0))
760  {
762  }
763 
764  protocol = &handles[protocol_handle];
765 
766  /* send a message with command code to query endpoint info to endpoint 0
767  */
768  {
769  uint8_t uQueryMessage[1] = { CNST_MSG_QUERY_FW_INFO };
770  send_message(protocol, 0, uQueryMessage, sizeof(uQueryMessage));
771  }
772 
773  /* read and parse reply message from connected device */
774  message_info.payload = NULL;
775  receive_status = get_message(protocol, &message_info);
776 
777  if ((receive_status != CNST_PROTOCOL_RECEIVED_PAYLOAD_MSG) ||
778  (message_info.endpoint != 0) ||
779  (message_info.payload_size < 7) ||
780  (message_info.payload[0] != CNST_MSG_FW_INFO))
781  {
782  /* This is not the expected payload, clean up and quit. */
783  free(message_info.payload);
785  }
786 
787  information->description = NULL;
788  information->version_major =
789  protocol_read_payload_uint16(message_info.payload, 1);
790  information->version_minor =
791  protocol_read_payload_uint16(message_info.payload, 3);
792  information->version_build =
793  protocol_read_payload_uint16(message_info.payload, 5);
794 
795  if (message_info.payload_size > 7)
796  {
797  uint16_t description_size = message_info.payload_size - 7;
798  information->description = malloc(description_size);
799  memcpy(information->description, message_info.payload + 7,
800  description_size);
801  }
802 
803  /* free payload memory */
804  free(message_info.payload);
805 
806  /* consume the expected status message */
807  receive_status = get_message(protocol, &message_info);
808 
809  if (receive_status != ((/*endpoint*/0 << 16) | /*status code*/0x0000))
810  {
811  /*
812  * If the next message was not a status message with Code 0, something
813  * went wrong, so remove all the endpoints, close interface and
814  * return.
815  */
816  if (receive_status == CNST_PROTOCOL_RECEIVED_PAYLOAD_MSG)
817  {
818  free(message_info.payload);
819  }
820 
822  }
823 
824  return 0;
825 }
826 
827 int32_t protocol_do_firmware_reset(int32_t protocol_handle)
828 {
829  Instance_t* protocol;
830  Message_Info_t message_info;
831  int32_t receive_status;
832 
833  /* check if handle is valid */
834  if ((protocol_handle < 0) ||
835  (protocol_handle >= (int32_t)num_allocated_handles) ||
836  (handles[protocol_handle].num_endpoints == 0))
837  {
839  }
840 
841  protocol = &handles[protocol_handle];
842 
843  /* send a message with command code to query endpoint info to endpoint 0
844  */
845  {
846  uint8_t uQueryMessage[1] = { CNST_MSG_FIRMWARE_RESET };
847  send_message(protocol, 0, uQueryMessage, sizeof(uQueryMessage));
848  }
849 
850  /* consume the expected status message */
851  receive_status = get_message(protocol, &message_info);
852 
853  if (receive_status != ((/*endpoint*/0 << 16) | /*status code*/0x0000))
854  {
855  /*
856  * If the next message was not a status message with Code 0, something
857  * went wrong, so remove all the endpoints, close interface and
858  * return.
859  */
860  if (receive_status == CNST_PROTOCOL_RECEIVED_PAYLOAD_MSG)
861  {
862  free(message_info.payload);
863  }
864 
866  }
867 
868  return 0;
869 }
870 
871 int32_t protocol_get_endpoint_info(int32_t protocol_handle, uint8_t endpoint,
872  Endpoint_Info_t* endpoint_info)
873 {
874  Endpoint_t* requested_endpoint;
875 
876  /* check if handle is valid */
877  if ((protocol_handle < 0) ||
878  (protocol_handle >= (int32_t)num_allocated_handles) ||
879  (handles[protocol_handle].num_endpoints == 0))
880  {
882  }
883 
884  /* check if endpoint exists */
885  if ((endpoint == 0) ||
886  (endpoint > handles[protocol_handle].num_endpoints))
887  {
889  }
891  /* write endpoint information to endpoint_info */
892  requested_endpoint = &handles[protocol_handle].endpoints[endpoint - 1];
893 
894  endpoint_info->type = requested_endpoint->type;
895  endpoint_info->version = requested_endpoint->version;
896  endpoint_info->description = "";
897 
898  /* get endpoint description, if it is a known endpoint type */
899  if (requested_endpoint->endpoint_definiton)
900  {
901  endpoint_info->description =
902  requested_endpoint->endpoint_definiton->description;
903  }
904 
905  return 0;
906 }
907 
908 int32_t protocol_is_endpoint_compatible(int32_t protocol_handle,
909  uint8_t endpoint,
910  const Endpoint_Definition_t*
911  endpoint_definiton)
912 {
913  Endpoint_Info_t sEndpointInfo;
914 
915  if (protocol_get_endpoint_info(protocol_handle, endpoint, &sEndpointInfo))
916  {
918  }
919 
920  if (sEndpointInfo.type != endpoint_definiton->type)
921  {
923  }
924 
925  if (sEndpointInfo.version < endpoint_definiton->min_version)
926  {
928  }
929 
930  if (sEndpointInfo.version > endpoint_definiton->max_version)
931  {
933  }
934 
935  return 0;
936 }
937 
938 int32_t protocol_send_and_receive(int32_t protocol_handle,
939  uint8_t endpoint,
940  const Endpoint_Definition_t*
941  endpoint_definiton,
942  const uint8_t* payload,
943  uint16_t payload_size)
944 {
945  Instance_t* protocol;
946  Message_Info_t message_info;
947  int32_t status_code;
948 
949  /* check handle and endpoint compatibility */
950  /* --------------------------------------- */
951  /* check handle */
952  if ((protocol_handle < 0) ||
953  (protocol_handle >= (int32_t)num_allocated_handles) ||
954  (handles[protocol_handle].num_endpoints == 0))
955  {
957  }
958 
959  protocol = &handles[protocol_handle];
960 
961  /* check if endpoint exists */
962  if ((endpoint == 0) || (endpoint > protocol->num_endpoints))
963  {
965  }
966 
967  /* endpoint has correct type and version */
968  if (protocol->endpoints[endpoint-1].endpoint_definiton !=
969  endpoint_definiton)
970  {
971  return protocol_is_endpoint_compatible(protocol_handle, endpoint,
972  endpoint_definiton);
973  }
974 
975  /* send message */
976  send_message(protocol, endpoint, payload, payload_size);
977 
978  /* receive messages from the board */
979  while ((status_code = get_message(protocol, &message_info)) ==
981  {
982  /* forward message to endpoint implementation */
983  if (message_info.endpoint > 0)
984  {
985  const Endpoint_Definition_t* endpoint_ptr =
986  protocol->endpoints[message_info.endpoint-1].endpoint_definiton;
987 
988  if (endpoint_ptr)
989  {
990  endpoint_ptr->parse_payload(protocol_handle,
991  message_info.endpoint,
992  message_info.payload,
993  message_info.payload_size);
994  }
995  }
996 
997  free(message_info.payload);
998  }
999 
1000  return status_code;
1001 }
1002 
1003 
1004 const char* protocol_get_status_code_description(int32_t protocol_handle,
1005  int32_t status_code)
1006 {
1007  // first check status code that are common to all endpoints
1008  if ((status_code & 0xFFFF) == PROTOCOL_STATUS_OK)
1009  {
1010  return "No Error.";
1011  }
1012  else if (status_code == PROTOCOL_STATUS_DEVICE_BAD_COMMAND)
1013  {
1014  return "The device received a message at endpoint 0 that could not be understood.";
1015  }
1016 
1017  if (status_code >= 0)
1018  {
1019  // if status code is positive, it's a status code sent from the device
1020  // -------------------------------------------------------------------
1021  uint8_t endpoint = (uint8_t)(status_code >> 16);
1022  uint16_t error_code = status_code & 0xFFFF;
1024  // endpoint 0 is the protocol itself, so handle error code right here
1025  if (endpoint == 0)
1026  {
1027  switch (error_code)
1028  {
1030  return "A time out occured during message transmission from host to device.";
1031 
1033  return "The device received a message with a bad start sequence.";
1034 
1036  return "The device received a message for a non existing endpoint.";
1037 
1039  return "The device received a data message without payload.";
1040 
1042  return "The device's memory is not sufficient to process the receive data message.";
1043 
1045  return "The device received a message with a bad end sequence.";
1046 
1047  default:
1048  return "Unknown Error";
1049  }
1050  }
1051  else
1052  {
1053  Endpoint_t* requested_endpoint;
1054 
1055  /* check if handle is valid, before accessing the endpoints */
1056  if ((protocol_handle < 0) ||
1057  (protocol_handle >= (int32_t)num_allocated_handles) ||
1058  (handles[protocol_handle].num_endpoints == 0))
1059  {
1060  return "Invalid Handle.";
1061  }
1062 
1063  /* check if endpoint exists and get a short cut */
1064  if (endpoint > handles[protocol_handle].num_endpoints)
1065  {
1066  return "Invalid Endpoint.";
1067  }
1068 
1069  requested_endpoint =
1070  &(handles[protocol_handle].endpoints[endpoint-1]);
1071 
1072  /* get error message from endpoint implementation */
1073  if (requested_endpoint->endpoint_definiton)
1074  {
1075  return requested_endpoint->endpoint_definiton->
1076  get_status_descr(error_code);
1077  }
1078 
1079  return "Unknown Error";
1080  }
1081  }
1082  else
1083  {
1084  // if status code is negative, an communication error has occurred
1085  // --------------------------------------------------------------
1086 
1087  switch (status_code)
1088  {
1090  return "The protocol handle is not a valid.";
1091 
1093  return "The specified COM port could not be opened.";
1094 
1096  return "The device at the specified COM port is not compatible with the protocol.";
1097 
1099  return "The device didn't send any message.";
1100 
1102  return "A timeout occurred while receiving a message from the device.";
1103 
1105  return "The device sent a message with a bad start sequence.";
1106 
1108  return "The device sent a message with a bad end sequence.";
1109 
1111  return "The requested endpoint does not exist.";
1112 
1114  return "The requested endpoint is not of the type checked for.";
1115 
1117  return "The requested endpoint's version is too old to be supported.";
1118 
1120  return "The requested endpoint's version is too new to be supported.";
1121 
1122  default:
1123  return "Unknown Error";
1124  }
1125  }
1126 }
1127 
1128 /*
1129  * TODO: provide the time-out as parameter, then the loop control get's much simpler!
1130  */
1131 
1132 int32_t protocol_poll_receive_once(int32_t protocol_handle)
1133 {
1134  Instance_t * protocol;
1135  Message_Info_t message_info;
1136  int32_t status_code;
1137 
1138  /* check handle and endpoint compatibility */
1139  /* --------------------------------------- */
1140  /* check handle */
1141  if ((protocol_handle < 0) ||
1142  (protocol_handle >= (int32_t)num_allocated_handles) ||
1143  (handles[protocol_handle].num_endpoints == 0))
1144  {
1146  }
1147 
1148  protocol = &handles[protocol_handle];
1149 
1150  /* receive messages from the board */
1151  status_code = get_message(protocol, &message_info); //com_port_handle
1152 
1153  if (status_code == CNST_PROTOCOL_RECEIVED_PAYLOAD_MSG)
1154  {
1155 
1156  /* forward message to endpoint implementation */
1157  if (message_info.endpoint > 0)
1158  {
1159  const Endpoint_Definition_t * endpoint_ptr =
1160  protocol->endpoints[message_info.endpoint - 1].endpoint_definiton;
1161 
1162  if (endpoint_ptr)
1163  {
1164  endpoint_ptr->parse_payload(protocol_handle,
1165  message_info.endpoint,
1166  message_info.payload,
1167  message_info.payload_size);
1168  }
1169  }
1170 
1171  free(message_info.payload);
1172 
1173  }
1174 
1175  return status_code;
1176 }
1177 
1178 /* --- End of File -------------------------------------------------------- */
Port_Info_t parse_port_name(const char *port_name)
Definition: Protocol.c:524
int32_t protocol_do_firmware_reset(int32_t protocol_handle)
This function triggers a firmware reset of the connected device.
Definition: Protocol.c:846
#define PROTOCOL_ERROR_ENDPOINT_VERSION_TOO_OLD
Definition: Protocol.h:235
#define PROTOCOL_ERROR_COULD_NOT_OPEN_COM_PORT
Definition: Protocol.h:173
size_t com_get_data(com_t *com_port, void *data, size_t num_requested_bytes)
This function reads data from an open COM port.
#define CNST_MSG_ENDPOINT_INFO
Definition: Protocol.c:82
struct com_s com_t
Definition: COMPort.h:51
void com_close(com_t *com_port)
This function closes the specified COM port.
#define PROTOCOL_STATUS_DEVICE_NO_PAYLOAD
Definition: Protocol.h:113
This structure contains information about the firmware.
Definition: Protocol.h:273
const char * description
Definition: Protocol.h:296
const char * protocol_get_status_code_description(int32_t protocol_handle, int32_t status_code)
This function returns a human readable description of a status or error code.
Definition: Protocol.c:1023
#define PROTOCOL_STATUS_DEVICE_BAD_PAYLOAD_END
Definition: Protocol.h:125
#define PROTOCOL_ERROR_RECEIVED_NO_MESSAGE
Definition: Protocol.h:191
#define CNST_MSG_QUERY_ENDPOINT_INFO
Definition: Protocol.c:75
#define PROTOCOL_ERROR_RECEIVED_BAD_MESSAGE_START
Definition: Protocol.h:205
#define PROTOCOL_STATUS_DEVICE_BAD_MESSAGE_START
Definition: Protocol.h:101
This file defines the API to access a serial communication port.
#define PROTOCOL_ERROR_DEVICE_NOT_COMPATIBLE
Definition: Protocol.h:178
uint16_t version_build
Definition: Protocol.h:279
int32_t protocol_get_endpoint_info(int32_t protocol_handle, uint8_t endpoint, Endpoint_Info_t *endpoint_info)
This function returns information about the requested endpoint.
Definition: Protocol.c:890
This file defines the basic API to access Infineon sensor devices.
#define CNST_END_OF_PAYLOAD
Definition: Protocol.c:67
#define PROTOCOL_STATUS_OK
Definition: Protocol.h:91
int32_t protocol_poll_receive_once(int32_t protocol_handle)
Definition: Protocol.c:1151
size_t com_send_data(com_t *com_port, const void *data, size_t num_bytes)
This function sends data through an open COM port.
int32_t protocol_send_and_receive(int32_t protocol_handle, uint8_t endpoint, const Endpoint_Definition_t *endpoint_definiton, const uint8_t *payload, uint16_t payload_size)
Definition: Protocol.c:957
int32_t protocol_get_num_endpoints(int32_t protocol_handle)
This function returns the number of endpoints in the connected device.
Definition: Protocol.c:755
int32_t protocol_connect(const char *port_name)
This function establishes a connection to a device through a COM port.
Definition: Protocol.c:573
uint16_t version_major
Definition: Protocol.h:277
int32_t protocol_get_firmware_information(int32_t protocol_handle, Firmware_Information_t *information)
This function returns the information about the firmware of the connected device.
Definition: Protocol.c:768
void protocol_disconnect(int32_t protocol_handle)
This function closes an open connection.
Definition: Protocol.c:723
#define PROTOCOL_ERROR_ENDPOINT_WRONG_TYPE
Definition: Protocol.h:228
This structure contains information about an endpoint.
Definition: Protocol.h:290
uint16_t version_minor
Definition: Protocol.h:278
#define PROTOCOL_STATUS_DEVICE_TIMEOUT
Definition: Protocol.h:94
#define PROTOCOL_STATUS_DEVICE_BAD_COMMAND
Definition: Protocol.h:132
uint32_t type
Definition: Protocol.h:292
#define CNST_STARTBYTE_DATA
Definition: Protocol.c:61
#define PROTOCOL_ERROR_RECEIVED_BAD_MESSAGE_END
Definition: Protocol.h:212
int32_t protocol_is_endpoint_compatible(int32_t protocol_handle, uint8_t endpoint, const Endpoint_Definition_t *endpoint_definiton)
Definition: Protocol.c:927
#define CNST_MSG_FW_INFO
Definition: Protocol.c:96
com_t * com_open(const char *port_name)
This functions opens the specified COM port.
#define PROTOCOL_ERROR_RECEIVED_TIMEOUT
Definition: Protocol.h:196
#define PROTOCOL_ERROR_ENDPOINT_VERSION_TOO_NEW
Definition: Protocol.h:244
#define PROTOCOL_STATUS_DEVICE_BAD_ENDPOINT_ID
Definition: Protocol.h:106
#define CNST_STARTBYTE_STATUS
Definition: Protocol.c:64
#define CNST_MSG_QUERY_FW_INFO
Definition: Protocol.c:89
#define PROTOCOL_STATUS_DEVICE_OUT_OF_MEMORY
Definition: Protocol.h:118
uint16_t version
Definition: Protocol.h:294
#define CNST_MSG_FIRMWARE_RESET
Definition: Protocol.c:103
#define PROTOCOL_ERROR_ENDPOINT_DOES_NOT_EXIST
Definition: Protocol.h:223
#define PROTOCOL_ERROR_INVALID_HANDLE
Definition: Protocol.h:158
#define CNST_PROTOCOL_RECEIVED_PAYLOAD_MSG
Definition: Protocol.c:109
#define NUM_KNOWN_ENDPOINTS
Definition: Protocol.c:254